<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:msds="http://matsadaml.org/msds/v1.0"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl msxsl"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	exclude-result-prefixes="exsl msxsl xsl xs msds xhtml">
	<xsl:output method="xml" encoding="UTF-8"
		indent="yes"/>
	<xsl:strip-space elements="*"/>
	<xsl:param name="CompanyDB"
		select="document('../../ResourceFiles/CompanyDB.xml')/msds:companyDB"/>
	<xsl:param name="Company"
		select="$CompanyDB/msds:company[@key = current()/msds:product/msds:company/@key]"/>
	<xsl:param name="Language"
		select="$Company/msds:custom-items/msds:item[@name = 'default-language']"/>
	<xsl:param name="Country"
		select="$Company/msds:custom-items/msds:item[@name = 'default-country']"/>
	<xsl:param name="SubstDBs">
		<xsl:variable name="DBs"
			select="document('../../Substances/SubstanceDBs.xml')/msds:substanceDBs"/>
		<xsl:for-each select="$DBs/msds:db">
			<msds:substanceDB key="{@key}">
				<xsl:copy-of select="document(msds:location)/msds:substanceDB/*"/>
			</msds:substanceDB>
		</xsl:for-each>
	</xsl:param>
	<xsl:variable name="SubstanceDBs" select="exsl:node-set($SubstDBs)"/>
	<!-- 
		
		<xsl:stylesheet version="1.0" xmlns:msds="http://matsadaml.org/msds/v1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
		
		<xsl:param name="SubstanceDBs" select="exsl:node-set($SubstDBs)"/>
		
		<xsl:choose>
		<xsl:when test="function-available('exsl:node-set')">
		<xsl:value-of select="exsl:node-set($SubstDBs)"/>
		</xsl:when>
		<xsl:otherwise>
		<xsl:value-of select="msxsl:node-set($SubstDBs)"/>
		</xsl:otherwise>
		</xsl:choose>
	-->
	<xsl:param name="RiskPhrases"
		select="document('../../ResourceFiles/RiskPhrases.xml')/msds:riskPhrases"/>
	<xsl:param name="SafetyPhrases"
		select="document('../../ResourceFiles/SafetyPhrases.xml')/msds:safetyPhrases"/>
	<xsl:param name="Hazards" select="document('../../ResourceFiles/Hazards.xml')/msds:hazards"/>
	<xsl:param name="ExposureLimitRemarks"
		select="document('../../ResourceFiles/ExposureLimitRemarks.xml')/msds:exposureLimitRemarks"/>
	<xsl:param name="Standard_TextTags" select="document('Standard_TextTags.xml')/textTags"/>
	<xsl:variable name="DecimalFormat"
		select="$Standard_TextTags/textTag[@key = 'DecimalFormat']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
	<xsl:decimal-format name="AngloSaxon" decimal-separator="." grouping-separator="," percent="%"
		minus-sign="-"/>
	<xsl:decimal-format name="WestEuropean" decimal-separator="," grouping-separator="." percent="%"
		minus-sign="-"/>
	<xsl:decimal-format name="NorthEuropean" decimal-separator="," grouping-separator=" "
		percent="%" minus-sign="-"/>
	<!--  -->
	<!--Root tag-->
	<!--  -->
	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>
	<!--  -->
	<!--product tag, start doing stuff-->
	<!--  -->
	<xsl:template match="msds:product">
		<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml">
			<fo:layout-master-set>
				<fo:simple-page-master master-name="leftPage" page-height="29.7cm" page-width="21cm" margin-left="1cm" margin-right="2cm" margin-top="1.5cm" margin-bottom="0.5cm">
					<fo:region-body margin-bottom="1cm"/>
					<fo:region-after extent="1cm"/>
				</fo:simple-page-master>
				<fo:simple-page-master master-name="rightPage" page-height="29.7cm" page-width="21cm" margin-left="2cm" margin-right="1cm" margin-top="1.5cm" margin-bottom="0.5cm">
					<fo:region-body margin-bottom="1cm"/>
					<fo:region-after extent="1cm"/>
				</fo:simple-page-master>
				<fo:page-sequence-master master-name="contents">
					<fo:repeatable-page-master-alternatives>
						<fo:conditional-page-master-reference master-reference="rightPage" odd-or-even="odd"/>
						<fo:conditional-page-master-reference master-reference="rightPage" odd-or-even="even"/>
					</fo:repeatable-page-master-alternatives>
				</fo:page-sequence-master>
			</fo:layout-master-set>
			<fo:page-sequence master-reference="contents" initial-page-number="1">
				<fo:static-content flow-name="xsl-region-after">
					<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="10pt" background-color="#FF9933" padding="3pt">
						<fo:block text-align="end">
							<fo:inline font-style="italic">
								<xsl:value-of select="$Standard_TextTags/textTag[@key = 'MSDSpublishedBy']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>: <xsl:value-of select="$Company/msds:contactDetails/msds:companyName"/>, </fo:inline>
							<xsl:value-of select="$Standard_TextTags/textTag[@key = 'Product']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>: <xsl:value-of select="//msds:name[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
						</fo:block>
						<fo:block text-align="end">
							<xsl:value-of select="$Standard_TextTags/textTag[@key = 'Page']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>: <fo:page-number/>
						</fo:block>
					</fo:block>
				</fo:static-content>
				<fo:flow flow-name="xsl-region-body">
					<fo:block background-color="#FF9933" padding="3pt">
						<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="14pt" font-weight="bold" space-before="6pt">
							<xsl:value-of select="$Standard_TextTags/textTag[@key = 'MaterialSafetyDataSheet']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
						</fo:block>
						<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="32pt" font-weight="bold" space-before="6pt">
							<xsl:value-of select="//msds:name[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
						</fo:block>
					</fo:block>
					<fo:block>
						<xsl:call-template name="IntroductionSection"/>
						<xsl:apply-templates select="msds:hazardsIdentification"/>
						<xsl:call-template name="CompositionSection"/>
						<xsl:apply-templates select="msds:firstAidMeasures"/>
						<xsl:apply-templates select="msds:fireFightingMeasures"/>
						<xsl:apply-templates select="msds:accidentalReleaseMeasures"/>
						<xsl:apply-templates select="msds:handlingAndStorage"/>
						<xsl:apply-templates select="msds:personalProtection"/>
						<xsl:apply-templates select="msds:properties"/>
						<xsl:apply-templates select="msds:stability"/>
						<xsl:apply-templates select="msds:toxicology"/>
						<xsl:apply-templates select="msds:ecotoxicology"/>
						<xsl:apply-templates select="msds:disposal"/>
						<xsl:apply-templates select="msds:transportation"/>
						<xsl:apply-templates select="msds:regulatoryInformation"/>
						<xsl:apply-templates select="msds:otherInformation"/>
					</fo:block>
				</fo:flow>
			</fo:page-sequence>
		</fo:root>
	</xsl:template>
	<!--  -->
	<!--product tag, create Introduction section-->
	<!--  -->
	<xsl:template name="IntroductionSection">
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="20pt" font-weight="900" background-color="#AAAAAA" space-before="6mm" space-after="1mm" padding="3pt" keep-with-next="always">
			<xsl:value-of select="$Standard_TextTags/textTag[@key = 'IntroductionTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
		</fo:block>
		<fo:block border="1pt solid #999999" font-size="10pt" padding="2pt" keep-with-previous="always" keep-together="always">
			<fo:table font-family="Verdana, Arial, Helvetica, sans-serif" table-layout="fixed" width="100%" keep-together="always">
				<fo:table-column column-width="4.5cm"/>
				<fo:table-column column-width="13cm"/>
				<fo:table-body keep-together="always">
					<fo:table-row space-before="5pt" space-after="8pt">
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<fo:inline font-weight="bold">
									<xsl:value-of select="$Standard_TextTags/textTag[@key = 'ProductName']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
								</fo:inline>
							</fo:block>
						</fo:table-cell>
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<xsl:value-of select="msds:name[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
							</fo:block>
						</fo:table-cell>
					</fo:table-row>
					<fo:table-row space-before="5pt" space-after="8pt">
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<fo:inline font-weight="bold">
									<xsl:value-of select="$Standard_TextTags/textTag[@key = 'Supplier']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
								</fo:inline>
							</fo:block>
						</fo:table-cell>
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<xsl:value-of select="$Company/msds:contactDetails/msds:companyName"/>
							</fo:block>
						</fo:table-cell>
					</fo:table-row>
					<fo:table-row space-before="5pt" space-after="8pt">
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<fo:inline font-weight="bold">
									<xsl:value-of select="$Standard_TextTags/textTag[@key = 'Address']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
								</fo:inline>
							</fo:block>
						</fo:table-cell>
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<xsl:value-of select="$Company/msds:contactDetails/msds:address1"/>, 
									<xsl:if test="$Company/msds:contactDetails/msds:address2">
									<xsl:value-of select="$Company/msds:contactDetails/msds:address2"/>, 
								</xsl:if>
								<xsl:if test="$Company/msds:contactDetails/msds:address3">
									<xsl:value-of select="$Company/msds:contactDetails/msds:address3"/>, 
								</xsl:if>
								<xsl:value-of select="$Company/msds:contactDetails/msds:postCode"/>, 
								<xsl:value-of select="$Company/msds:contactDetails/msds:city"/>, 
								<xsl:value-of select="$Company/msds:contactDetails/msds:country"/>
							</fo:block>
						</fo:table-cell>
					</fo:table-row>
					<fo:table-row space-before="5pt" space-after="8pt">
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<fo:inline font-weight="bold">
									<xsl:value-of select="$Standard_TextTags/textTag[@key = 'Telephone']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
								</fo:inline>
							</fo:block>
						</fo:table-cell>
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<xsl:value-of select="$Company/msds:contactDetails/msds:telephone"/>
							</fo:block>
						</fo:table-cell>
					</fo:table-row>
				</fo:table-body>
			</fo:table>
			<xsl:variable name="paragraphs" select="msds:information/msds:paragraph[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
			<xsl:if test="$paragraphs">
				<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0"/>
				<xsl:call-template name="CreateParagraphTable">
					<xsl:with-param name="Paragraphs" select="$paragraphs"/>
				</xsl:call-template>
			</xsl:if>
		</fo:block>
	</xsl:template>
	<!--  -->
	<!-- create section for hazards -->
	<!--  -->
	<xsl:template match="msds:hazardsIdentification">
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="20pt" font-weight="900" background-color="#AAAAAA" space-before="6mm" space-after="1mm" padding="3pt" keep-with-next="always">
			<xsl:value-of select="$Standard_TextTags/textTag[@key = 'HazardsTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
		</fo:block>
		<fo:block border="1pt solid #999999" font-size="10pt" padding="2pt" keep-with-previous="always" keep-together="always">
			<xsl:if test="//msds:regulatoryInformation/msds:hazardSymbols/msds:hazardSymbol">
				<xsl:apply-templates select="//msds:regulatoryInformation/msds:hazardSymbols"/>
			</xsl:if>
			<xsl:variable name="paragraphs" select="msds:paragraph[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
			<xsl:if test="$paragraphs">
				<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0"/>
				<xsl:call-template name="CreateParagraphTable">
					<xsl:with-param name="Paragraphs" select="$paragraphs"/>
				</xsl:call-template>
			</xsl:if>
		</fo:block>
	</xsl:template>
	<!--  -->
	<!-- create section for composition-->
	<!--  -->
	<xsl:template name="CompositionSection">
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="20pt" font-weight="900" background-color="#AAAAAA" space-before="6mm" space-after="1mm" padding="3pt" keep-with-next="always">
			<xsl:value-of select="$Standard_TextTags/textTag[@key = 'CompositionTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
		</fo:block>
		<fo:block border="1pt solid #666666" background-color="#BBBBBB" font-size="10pt" padding="2pt" keep-with-previous="always" keep-together="always">
			<xsl:if test="msds:substances/msds:substance">
				<fo:table font-family="Verdana, Arial, Helvetica, sans-serif" table-layout="fixed" width="100%" keep-together="always">
					<fo:table-column column-width="5cm"/>
					<fo:table-column column-width="2.5cm"/>
					<fo:table-column column-width="2cm"/>
					<fo:table-column column-width="2cm"/>
					<fo:table-column column-width="2cm"/>
					<fo:table-column column-width="2cm"/>
					<fo:table-column column-width="2cm"/>
					<fo:table-column column-width="2cm"/>
					<fo:table-body keep-together="always">
						<fo:table-row space-before="5pt" space-after="8pt">
							<xsl:for-each select="$Standard_TextTags/textTag[@key = 'CompositionTableHeader']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]/textTag">
								<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
									<fo:block>
										<fo:inline font-weight="bold">
											<xsl:value-of select="."/>
										</fo:inline>
									</fo:block>
								</fo:table-cell>
							</xsl:for-each>
						</fo:table-row>
						<xsl:for-each select="msds:substances/msds:substance">
							<fo:table-row space-before="5pt" space-after="8pt">
								<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
									<fo:block>
										<xsl:value-of select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:names/msds:name[@lang = $Language]"/>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
									<fo:block>
										<xsl:value-of select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:cas-number"/>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
									<fo:block>
										<xsl:value-of select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:eg-number"/>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
									<fo:block>
										<xsl:for-each select="$Hazards/msds:hazard[$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:hazardCodes/msds:hazardCode/@key=@key]">
											<xsl:sort select="msds:code"/>
											<xsl:value-of select="msds:code"/>
											<xsl:if test="not(position()=last())">
												<xsl:text>, </xsl:text>
											</xsl:if>
										</xsl:for-each>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
									<fo:block>
										<xsl:for-each select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:riskPhrases/msds:riskPhrase">
											<xsl:sort select="$RiskPhrases/msds:riskPhrase[@key=current()/@key]/msds:code"/>
											<xsl:value-of select="$RiskPhrases/msds:riskPhrase[@key=current()/@key]/msds:code"/>
											<xsl:if test="not(position()=last())">
												<xsl:text>, </xsl:text>
											</xsl:if>
										</xsl:for-each>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
									<fo:block>
										<xsl:call-template name="FormatNumber">
											<xsl:with-param name="Value" select="msds:concentration/msds:min"/>
											<xsl:with-param name="FormatString" select="$Standard_TextTags/textTag[@key = 'FormatString_0']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
										</xsl:call-template>
										<xsl:value-of select="' '"/>
										<xsl:value-of select="msds:concentration/msds:min/@unit"/>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
									<fo:block>
										<xsl:call-template name="FormatNumber">
											<xsl:with-param name="Value" select="msds:concentration/msds:max"/>
											<xsl:with-param name="FormatString" select="$Standard_TextTags/textTag[@key = 'FormatString_0']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
										</xsl:call-template>
										<xsl:value-of select="' '"/>
										<xsl:value-of select="msds:concentration/msds:max/@unit"/>
									</fo:block>
								</fo:table-cell>
							</fo:table-row>
						</xsl:for-each>
					</fo:table-body>
				</fo:table>
			</xsl:if>
			<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0">
				<fo:inline font-weight="bold">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'CompositionRefComment']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</fo:inline>
			</fo:block>
			<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0">
				<fo:inline font-weight="bold">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'HazardousCodesExplanation']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
					<xsl:text> </xsl:text>
				</fo:inline>
				<xsl:variable name="unique-hazardCode" select="$Hazards/msds:hazard[$SubstanceDBs/msds:substanceDB[@key = current()/msds:substances/msds:substance/@db]/msds:substance-entry[@id=current()/msds:substances/msds:substance/@id]/msds:hazardCodes/msds:hazardCode/@key=@key]"/>
				<xsl:for-each select="$unique-hazardCode">
					<xsl:sort select="msds:code"/>
					<xsl:value-of select="msds:code"/>
					<xsl:text>=</xsl:text>
					<xsl:value-of select="msds:text[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
					<xsl:if test="not(position()=last())">
						<xsl:text>, </xsl:text>
					</xsl:if>
				</xsl:for-each>
			</fo:block>
			<xsl:variable name="paragraphs" select="msds:comment/msds:paragraph[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
			<xsl:if test="$paragraphs">
				<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="12pt 0 2pt 0" keep-with-previous="always" keep-together="always">
					<xsl:call-template name="CreateParagraphTable">
						<xsl:with-param name="Paragraphs" select="$paragraphs"/>
					</xsl:call-template>
				</fo:block>
			</xsl:if>
		</fo:block>
	</xsl:template>
	<!--  -->
	<!-- tags with paragraphs or steps, create section headers and table beneath. Except personalProtection tag-->
	<!--  -->
	<xsl:template match="msds:firstAidMeasures | msds:fireFightingMeasures | msds:accidentalReleaseMeasures | msds:handlingAndStorage | msds:properties | msds:stability | msds:toxicology | msds:ecotoxicology | msds:disposal | msds:transportation">
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="20pt" font-weight="900" background-color="#AAAAAA" space-before="6mm" space-after="1mm" padding="3pt" keep-with-next="always">
			<xsl:choose>
				<xsl:when test="local-name(.) = 'firstAidMeasures'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'FirstAidTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'fireFightingMeasures'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'FireFightingTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'accidentalReleaseMeasures'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'AccidentalReleaseTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'handlingAndStorage'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'HandlingStorageTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'properties'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'PropertiesTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'stability'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'StabilityTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'toxicology'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'ToxicologicalTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'ecotoxicology'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'EcologicalTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'disposal'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'DisposalTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
				<xsl:when test="local-name() = 'transportation'">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'TransportTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:when>
			</xsl:choose>
		</fo:block>
		<xsl:variable name="paragraphs" select="msds:paragraph[contains(@countries, concat($Country, '; ')) and @lang = $Language] | msds:step[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
		<xsl:if test="$paragraphs">
			<fo:block border="1pt solid #999999" font-size="10pt" padding="2pt" keep-with-previous="always" keep-together="always">
				<xsl:call-template name="CreateParagraphTable">
					<xsl:with-param name="Paragraphs" select="$paragraphs"/>
				</xsl:call-template>
			</fo:block>
		</xsl:if>
	</xsl:template>
	<!--  -->
	<!-- personalProtection tag, note here will also be a table based on the substances tag-->
	<!--  -->
	<xsl:template match="msds:personalProtection">
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="20pt" font-weight="900" background-color="#AAAAAA" space-before="6mm" space-after="1mm" padding="3pt" keep-with-next="always">
			<xsl:value-of select="$Standard_TextTags/textTag[@key = 'ExposureControlTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
		</fo:block>
		<fo:block border="1pt solid #666666" background-color="#BBBBBB" font-size="10pt" padding="2pt" keep-with-previous="always" keep-together="always">
			<fo:table font-family="Verdana, Arial, Helvetica, sans-serif" table-layout="fixed" keep-together="always">
				<fo:table-column column-width="5cm"/>
				<fo:table-column column-width="3cm"/>
				<fo:table-column column-width="1.7cm"/>
				<fo:table-column column-width="1.7cm"/>
				<fo:table-column column-width="1.7cm"/>
				<fo:table-column column-width="1.7cm"/>
				<fo:table-column column-width="1.5cm"/>
				<fo:table-column column-width="1.5cm"/>
				<fo:table-body keep-together="always">
					<fo:table-row space-before="5pt" space-after="8pt">
						<xsl:for-each select="$Standard_TextTags/textTag[@key = 'ExposureControlsTableHeader']/textTag[@key = 'FirstHeaderRow']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]/textTag">
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<fo:inline font-weight="bold">
										<xsl:apply-templates select="."/>
									</fo:inline>
								</fo:block>
							</fo:table-cell>
						</xsl:for-each>
					</fo:table-row>
					<fo:table-row space-before="5pt" space-after="8pt">
						<xsl:for-each select="$Standard_TextTags/textTag[@key = 'ExposureControlsTableHeader']/textTag[@key = 'SecondHeaderRow']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]/textTag">
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<fo:inline font-weight="bold">
										<xsl:apply-templates select="."/>
									</fo:inline>
								</fo:block>
							</fo:table-cell>
						</xsl:for-each>
					</fo:table-row>
					<xsl:for-each select="../msds:substances/msds:substance">
						<fo:table-row space-before="5pt" space-after="8pt">
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<xsl:value-of select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:names/msds:name[@lang = $Language]"/>
								</fo:block>
							</fo:table-cell>
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<xsl:value-of select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:cas-number"/>
								</fo:block>
							</fo:table-cell>
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<xsl:variable name="Value1" select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:exposureLimit/msds:item[@name = 'nivågränsvärde' and @unit = 'ppm']"/>
									<xsl:if test="$Value1/text()">
										<xsl:call-template name="FormatNumber">
											<xsl:with-param name="Value" select="$Value1"/>
											<xsl:with-param name="FormatString" select="$Standard_TextTags/textTag[@key = 'FormatString_0']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
										</xsl:call-template>
									</xsl:if>
								</fo:block>
							</fo:table-cell>
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<xsl:variable name="Value2" select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:exposureLimit/msds:item[@name = 'nivågränsvärde' and @unit = 'mgm3']"/>
									<xsl:if test="$Value2/text()">
										<xsl:call-template name="FormatNumber">
											<xsl:with-param name="Value" select="$Value2"/>
											<xsl:with-param name="FormatString" select="$Standard_TextTags/textTag[@key = 'FormatString_0']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
										</xsl:call-template>
									</xsl:if>
								</fo:block>
							</fo:table-cell>
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<xsl:variable name="Value3" select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:exposureLimit/msds:item[@name = 'korttidsvärde' and @unit = 'ppm']"/>
									<xsl:if test="$Value3/text()">
										<xsl:call-template name="FormatNumber">
											<xsl:with-param name="Value" select="$Value3"/>
											<xsl:with-param name="FormatString" select="$Standard_TextTags/textTag[@key = 'FormatString_0']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
										</xsl:call-template>
									</xsl:if>
								</fo:block>
							</fo:table-cell>
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<xsl:variable name="Value4" select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:exposureLimit/msds:item[@name = 'korttidsvärde' and @unit = 'mgm3']"/>
									<xsl:if test="$Value4/text()">
										<xsl:call-template name="FormatNumber">
											<xsl:with-param name="Value" select="$Value4"/>
											<xsl:with-param name="FormatString" select="$Standard_TextTags/textTag[@key = 'FormatString_0']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
										</xsl:call-template>
									</xsl:if>
								</fo:block>
							</fo:table-cell>
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<!-- this printout must be updated when remarks for other countries and languages appear in product-xml-->
									<xsl:value-of select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:exposureLimit/msds:item[@name = 'remark']"/>
								</fo:block>
							</fo:table-cell>
							<fo:table-cell border="1px solid #666666" padding="1pt" display-align="after">
								<fo:block>
									<!-- this printout must be updated when remarks for other countries and languages appear in product-xml-->
									<xsl:value-of select="$SubstanceDBs/msds:substanceDB[@key = current()/@db]/msds:substance-entry[@id=current()/@id]/msds:exposureLimit/msds:item[@name = 'year']"/>
								</fo:block>
							</fo:table-cell>
						</fo:table-row>
					</xsl:for-each>
				</fo:table-body>
			</fo:table>
			<xsl:variable name="unique-remark" select="$ExposureLimitRemarks/msds:remark[msds:code = $SubstanceDBs/msds:substanceDB[@key = current()/ancestor::*/descendant::msds:substance/@db]/msds:substance-entry[@id=current()/ancestor::*/descendant::msds:substance/@id]/msds:exposureLimit/msds:item[contains(@countries, concat($Country, '; ')) and @name = 'remark']]"/>
			<xsl:if test="$unique-remark">
				<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0">
					<fo:inline font-weight="bold">
						<xsl:value-of select="$Standard_TextTags/textTag[@key = 'ExplanationNote']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
					</fo:inline>
					<xsl:for-each select="$unique-remark">
						<xsl:sort select="msds:code"/>
						<xsl:value-of select="msds:code"/>
						<xsl:text>=</xsl:text>
						<xsl:value-of select="msds:text[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
						<xsl:if test="not(position()=last())">
							<xsl:text>, </xsl:text>
						</xsl:if>
					</xsl:for-each>
				</fo:block>
			</xsl:if>
			<fo:inline font-style="italic">
				<xsl:value-of select="$Standard_TextTags/textTag[@key = 'ExposureControlsExplanation']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
			</fo:inline>
		</fo:block>
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0"/>
		<xsl:variable name="paragraphs" select="msds:paragraph[contains(@countries, concat($Country, '; ')) and @lang = $Language] | msds:step[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
		<xsl:if test="$paragraphs">
			<fo:block border="1pt solid #999999" font-size="10pt" padding="2pt" keep-with-previous="always" keep-together="always">
				<xsl:call-template name="CreateParagraphTable">
					<xsl:with-param name="Paragraphs" select="$paragraphs"/>
				</xsl:call-template>
			</fo:block>
		</xsl:if>
	</xsl:template>
	<!--  -->
	<!-- regulatoryInformation tag-->
	<!--  -->
	<xsl:template match="msds:regulatoryInformation">
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="20pt" font-weight="900" background-color="#AAAAAA" space-before="6mm" space-after="1mm" padding="3pt" keep-with-next="always">
			<xsl:value-of select="$Standard_TextTags/textTag[@key = 'RegulatoryTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
		</fo:block>
		<fo:block border="1pt solid #999999" font-size="10pt" padding="2pt" keep-with-previous="always" keep-together="always">
			<xsl:if test="msds:hazardSymbols/msds:hazardSymbol">
				<xsl:apply-templates select="msds:hazardSymbols"/>
			</xsl:if>
			<xsl:if test="msds:riskPhrases/msds:riskPhrase">
				<xsl:call-template name="CreateItemList">
					<xsl:with-param name="Items" select="$RiskPhrases/msds:riskPhrase[@key=current()/msds:riskPhrases/msds:riskPhrase/@key]"/>
					<xsl:with-param name="Caption" select="$Standard_TextTags/textTag[@key = 'RiskPhrases']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:call-template>
			</xsl:if>
			<xsl:if test="msds:safetyPhrases/msds:safetyPhrase">
				<xsl:call-template name="CreateItemList">
					<xsl:with-param name="Items" select="$SafetyPhrases/msds:safetyPhrase[@key=current()/msds:safetyPhrases/msds:safetyPhrase/@key]"/>
					<xsl:with-param name="Caption" select="$Standard_TextTags/textTag[@key = 'SafetyPhrases']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:call-template>
			</xsl:if>
			<xsl:variable name="paragraphs" select="msds:paragraph[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
			<xsl:if test="$paragraphs">
				<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0"/>
				<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-weight="bold" space-before="1mm">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'OtherInformation']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
					<xsl:text>: </xsl:text>
				</fo:block>
				<xsl:call-template name="CreateParagraphTable">
					<xsl:with-param name="Paragraphs" select="$paragraphs"/>
				</xsl:call-template>
			</xsl:if>
		</fo:block>
	</xsl:template>
	<!--  -->
	<!-- OtherInformation tag-->
	<!--  -->
	<xsl:template match="msds:otherInformation">
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-size="20pt" font-weight="900" background-color="#AAAAAA" space-before="6mm" space-after="1mm" padding="3pt" keep-with-next="always">
			<xsl:value-of select="$Standard_TextTags/textTag[@key = 'OtherInformationTitle']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
		</fo:block>
		<fo:block border="1pt solid #999999" font-size="10pt" padding="2pt" keep-with-previous="always" keep-together="always">
			<xsl:variable name="risks" select="$RiskPhrases/msds:riskPhrase[$SubstanceDBs/msds:substanceDB[@key = current()/ancestor::*/descendant::msds:substance/@db]/msds:substance-entry[@id=current()/ancestor::*/descendant::msds:substance/@id]/msds:riskPhrases/msds:riskPhrase/@key = @key]"/>
			<xsl:if test="$risks">
				<xsl:call-template name="CreateItemList">
					<xsl:with-param name="Items" select="$risks"/>
					<xsl:with-param name="Caption" select="$Standard_TextTags/textTag[@key = 'RiskPhrasesFromParagraph3']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
				</xsl:call-template>
			</xsl:if>
			<xsl:variable name="paragraphs" select="msds:paragraph[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
			<xsl:if test="$paragraphs">
				<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0"/>
				<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-weight="bold" space-before="1mm">
					<xsl:value-of select="$Standard_TextTags/textTag[@key = 'OtherInformation']/textTag[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
					<xsl:text>: </xsl:text>
				</fo:block>
				<xsl:call-template name="CreateParagraphTable">
					<xsl:with-param name="Paragraphs" select="$paragraphs"/>
				</xsl:call-template>
			</xsl:if>
		</fo:block>
	</xsl:template>
	<!--  -->
	<!-- create hazardSymbols table-->
	<!--  -->
	<xsl:template match="msds:hazardSymbols">
		<fo:table font-family="Verdana, Arial, Helvetica, sans-serif" table-layout="fixed" width="100%" keep-together="always">
			<fo:table-column column-width="3cm"/>
			<fo:table-column column-width="3cm"/>
			<fo:table-column column-width="3cm"/>
			<fo:table-column column-width="3cm"/>
			<fo:table-column column-width="3cm"/>
			<fo:table-column column-width="3cm"/>
			<fo:table-column column-width="3cm"/>
			<fo:table-body keep-together="always">
				<fo:table-row space-before="5pt" space-after="8pt">
					<xsl:for-each select="//msds:regulatoryInformation/msds:hazardSymbols/msds:hazardSymbol/@key">
						<fo:table-cell padding="1pt" text-align="center">
							<fo:block>
								<fo:external-graphic content-width="1.8cm" content-height="1.8cm" src="../../ResourceFiles/Images/{$Hazards/msds:hazard[@key=current()]/msds:symbol}"/>
							</fo:block>
						</fo:table-cell>
					</xsl:for-each>
				</fo:table-row>
				<fo:table-row space-before="5pt" space-after="8pt">
					<xsl:for-each select="//msds:regulatoryInformation/msds:hazardSymbols/msds:hazardSymbol/@key">
						<fo:table-cell padding="1pt" text-align="center">
							<fo:block>
								<xsl:value-of select="$Hazards/msds:hazard[@key=current()]/msds:text[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
							</fo:block>
						</fo:table-cell>
					</xsl:for-each>
				</fo:table-row>
			</fo:table-body>
		</fo:table>
	</xsl:template>
	<!--  -->
	<!-- CreateItemList template-->
	<!--  -->
	<xsl:template name="CreateItemList">
		<xsl:param name="Items"/>
		<xsl:param name="Caption"/>
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0"/>
		<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" font-weight="bold" space-before="1mm">
			<xsl:value-of select="$Caption"/>
			<xsl:text>: </xsl:text>
		</fo:block>
		<xsl:for-each select="$Items">
			<xsl:sort select="msds:code"/>
			<fo:block font-family="Verdana, Arial, Helvetica, sans-serif" padding="2pt 0 2pt 0">
				<xsl:value-of select="msds:code"/>
				<xsl:text>  </xsl:text>
				<xsl:value-of select="msds:text[contains(@countries, concat($Country, '; ')) and @lang = $Language]"/>
			</fo:block>
		</xsl:for-each>
	</xsl:template>
	<!--  -->
	<!-- tags with paragraphs or steps, create section headers and table beneath-->
	<!--  -->
	<xsl:template name="CreateParagraphTable">
		<xsl:param name="Paragraphs"/>
		<fo:table font-family="Verdana, Arial, Helvetica, sans-serif" table-layout="fixed" width="100%" keep-together="always">
			<fo:table-column column-width="4.5cm"/>
			<fo:table-column column-width="13cm"/>
			<fo:table-body keep-together="always">
				<xsl:for-each select="$Paragraphs">
					<xsl:sort select="@pos"/>
					<fo:table-row space-before="5pt" space-after="8pt">
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<fo:inline font-weight="bold">
									<xsl:apply-templates select="msds:title"/>
								</fo:inline>
							</fo:block>
						</fo:table-cell>
						<fo:table-cell padding="2pt 0 2pt 0">
							<fo:block>
								<xsl:apply-templates select="msds:text"/>
							</fo:block>
						</fo:table-cell>
					</fo:table-row>
				</xsl:for-each>
			</fo:table-body>
		</fo:table>
	</xsl:template>
	<!--  -->
	<!-- FormatNumber template-->
	<!--  -->
	<xsl:template name="FormatNumber">
		<xsl:param name="Value"/>
		<xsl:param name="FormatString"/>
		<xsl:value-of select="format-number($Value, $FormatString, $DecimalFormat)"/>
	</xsl:template>
	<!--  -->
	<!-- Remove white-space template-->
	<!--  -->
	<xsl:template match="text()">
		<xsl:value-of select="normalize-space(.)"/>
	</xsl:template>
	<!--  -->
	<!-- NO BLOCKING TEMPLATE!! replace safsf with * to switch blocking on-->
	<!--  -->
	<xsl:template match="safsf"/>
	<!--  -->
	<!-- xhtml text formatting, more to be added?-->
	<!--  -->
	<xsl:template match="xhtml:tt">
		<fo:inline font-family="monospace">
			<xsl:apply-templates/>
		</fo:inline>
	</xsl:template>
	<xsl:template match="xhtml:pre">
		<fo:block white-space-collapse="false">
			<xsl:apply-templates/>
		</fo:block>
	</xsl:template>
	<xsl:template match="xhtml:b">
		<fo:inline font-weight="bold">
			<xsl:apply-templates/>
		</fo:inline>
	</xsl:template>
	<xsl:template match="xhtml:sup">
		<fo:inline vertical-align="super">
			<xsl:apply-templates/>
		</fo:inline>
	</xsl:template>
	<xsl:template match="xhtml:sub">
		<fo:inline vertical-align="sub">
			<xsl:apply-templates/>
		</fo:inline>
	</xsl:template>
	<xsl:template match="xhtml:i">
		<fo:inline font-style="italic">
			<xsl:apply-templates/>
		</fo:inline>
	</xsl:template>
	<xsl:template match="xhtml:hr">
		<fo:block>
			<fo:leader leader-pattern="rule" leader-length.optimum="100%" rule-style="double" rule-thickness="1pt"/>
		</fo:block>
	</xsl:template>
	<xsl:template match="xhtml:br">
		<fo:block>
			<xsl:text>&#xA;</xsl:text>
		</fo:block>
	</xsl:template>
</xsl:stylesheet>
